<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
	<meta charset="utf-8" />
		<title>业务台账变更申请</title>		
		
			<script type="text/javascript" src="plugins/websocketInstantMsg/ext4/ext-all-debug.js"></script>
<script type="text/javascript" src="static/js/jquery-1.7.2.js" />
			<script type="text/javascript" src="plugins/websocketInstantMsg/ext4/locale/ext-lang-zh_CN.js"></script>
			<link rel="stylesheet" href="plugins/websocketInstantMsg/ext4/resources/css/ext-all.css" />
			
			
</head>
<body>
<div id = "payment"></div>
<div id = "payment1"></div>
<div id = "payment2"></div>
</body>
<script type="text/javascript">
Ext.onReady(function(){
	var loan = Ext.create('Ext.data.Store', {
		fields: ["bill_code",
		         "loan_amount",
		         "currency"],
		pageSize:10,
		id:'loan',
		proxy: {
			type: 'ajax',
			url: '<%=basePath%>businessAccountApply/findBusinessAccountApplyInfo',
			reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
				type: 'json', //返回数据类型为json格式
				root: 'result.list',  //数据
				totalProperty: 'result.total' //数据总条数
			}
		},
		
	});
	var appCode = Ext.create('Ext.data.Store', {
		fields: ["payment_app_code",
		         "settlement_dept",
		         "payment_app_dept",
		         "dept_business_type",
		         "due_bank",
		         "state"],
		pageSize:10,
		id:'loan',
		proxy: {
			type: 'ajax',
			url: '<%=basePath%>businessAccountApply/findAppCodeInfo',
			reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
				type: 'json', //返回数据类型为json格式
				root: 'result.list',  //数据
				totalProperty: 'result.total' //数据总条数
			}
		},
		sorters: [{
			property: 'ordeId',
			direction: 'desc'
		}],
		autoLoad:{start:0,limit:10},
	});
	var store = Ext.create('Ext.data.Store', {
		fields: ["bill_code",
		         "amount"],
		pageSize:10,
		id:'store',
		proxy: {
			type: 'ajax',
			url: '<%=basePath%>businessAccountApply/findBusinessAccountApplyInfo',
			reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
				type: 'json', //返回数据类型为json格式
				root: 'result.list',  //数据
				totalProperty: 'result.total' //数据总条数
			}
		},
		
	});
	var currency = Ext.create('Ext.data.Store', {
		fields: ['id', 'name'],
		data : [
			{"id":"", "name":"请选择"},
			{"id":"1", "name":"借款"},
			{"id":"0", "name":"收入"}
		]
	});
	  var c1 = Ext.create('Ext.container.Container', {
			layout: 'column',
			defaults: { labelAlign: 'left', labelWidth: 60 },
			items: [
// 				{id:"startDate1",xtype: 'textfield', name: 'startDate1',columnWidth: 1/5,padding: '5px',fieldLabel: '申请号'},
// 				{layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 4/5,   padding: '10px'},
// 				{layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 4/5,   padding: '10px'},
// 				{id:"startDate2",xtype: 'combo', name: 'startDate2', columnWidth:  1/5,padding: '5px', fieldLabel: '业务编号'},
				
				
				{id:"startDate1",xtype: 'textfield', labelWidth: 90,name: 'startDate1',columnWidth: 1/5,store:currency,padding: '5px',fieldLabel: '收（付）款单号',listeners : {//选择一行后触发的事件 
//                     'select' : function(combo,record,index) { 
//                     	loan.on('beforeload', function(store,options) {  
//             		            var new_params = {  
//             		            		state:record[0].data.id
//             		            };  

//             		            Ext.apply(loan.proxy.extraParams,new_params);  
//             		        });  
//                     	loan.load();  
//                     }
                    }
                    },
                    {xtype: "button",columnWidth:  1/15,padding: '5px',icon:'<%=basePath%>plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "查询导入",handler: function(){
                    	
                    	fandDao.show();
                    	
                    }},
                    {layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 5/5,   padding: '10px'},
//     				{layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 4/5,   padding: '10px'},
				{id:"startDate2",xtype: 'combo', labelWidth: 70,name: 'startDate2',columnWidth: 1/5,store:loan,padding: '5px',displayField:'bill_code',valueField:'loan_amount',fieldLabel: '业务编号',listeners : {//选择一行后触发的事件 
                    'select' : function(combo,record,index) { 
                    	var v =record[0].data.loan_amount;
                    	Ext.getCmp('startDate3').setValue(v);
                    	Ext.getCmp('startDate31').setValue(record[0].data.currency);
                    }}
                    },
				{id:"startDate3",xtype: 'textfield', name: 'startDate3', columnWidth:  1/5,padding: '5px', fieldLabel: '预付金额'},
				{id:"startDate31",xtype: 'textfield', name: 'startDate31', columnWidth:  1/5,padding: '5px', fieldLabel: '币种'},
				{id:"startDate32",xtype: 'textfield', name: 'startDate32', columnWidth:  1/5,padding: '5px', fieldLabel: '类型',hidden:true},
				]
		});
	  var c2 = Ext.create('Ext.container.Container', {
			layout: 'column',
			defaults: { labelAlign: 'left', labelWidth: 60 },
			items: [
				{id:"startDate4",xtype: 'textfield', name: 'startDate4', columnWidth:  1/5,padding: '5px', fieldLabel: '金额'},
				{id:"startDate5",xtype: 'textarea', name: 'startDate5', columnWidth:  1/5,padding: '5px', fieldLabel: '申请变更原因'},
				{layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 4/5,   padding: '10px'},
				{layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 1/5,   padding: '10px'},
// 				{layout : 'column',baseCls : 'my-panel-no-border', columnWidth: 4/5,   padding: '10px'},
				{xtype: "button",columnWidth:  1/15,padding: '5px',icon:'<%=basePath%>plugins/websocketInstantMsg/ext4/shared/icons/fam/add.png', text: "添加",handler: function(){
					if(Ext.getCmp('startDate2').getValue()==null || Ext.getCmp('startDate2').getValue()==""){
						Ext.MessageBox.alert("提示","请选择业务编号"); 
						return false;
					}
					if(Ext.getCmp('startDate4').getValue()==null || Ext.getCmp('startDate4').getValue()==""){
						Ext.MessageBox.alert("提示","请填写金额"); 
						return false;
					}
					store.insert(store.getCount(),{
	       				xh:store.getCount(),bill_code:Ext.getCmp('startDate2').getRawValue(),amount:Ext.getCmp('startDate4').getValue()
	       			});
					Ext.getCmp('startDate4').setValue(null);
				}}
				]
		});
	var grid = Ext.create('Ext.grid.Panel', {
		renderTo:'payment2',
		store: store,
		id:'grid',
		forceFit: true,
		height: 400,
		title:'拆分记录',
		stripeRows:true,
		columnLines: true,
// 		plugins:[invoice_unit_cellEditing],
		selModel:Ext.create('Ext.selection.CheckboxModel',{mode:"single", allowDeselect: true, showHeaderCheckbox:false}),
		columns: [
			{text: 'check_id', dataIndex: 'check_id',hidden:true},
			{text: '业务编号', dataIndex: 'bill_code'},
			{text: '金额', dataIndex: 'amount'},
		],
		bbar: [{
			xtype: 'pagingtoolbar',
// 			store: store,
			displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
			emptyMsg: "没有数据",
			beforePageText: "当前页",
			afterPageText: "共{0}页",
			displayInfo: true
		}],

		tbar: [{text: '保存', icon: '<%=basePath%>plugins/websocketInstantMsg/ext4/shared/icons/fam/cog_edit.png', handler: function (){
			var aa= grid.getStore()  
			  var comValue = 0;
			  for(var i=0;i<aa.getCount();i++)
			  {
			     comValue+=parseInt(grid.getStore().data.items[i].data.amount);
			  }
			  if(comValue>Ext.getCmp('startDate3').getValue()){
				  Ext.MessageBox.alert("提示","金额合计不能大于拆分金额"); 
					return false;
			  }
			  if(comValue<Ext.getCmp('startDate3').getValue()){
				  Ext.MessageBox.alert("提示","金额合计不能小于于拆分金额"); 
					return false;
			  }
	      	 var app_change_reason= Ext.getCmp("startDate5").getValue(); 
	      	 var advance_pay_amount = Ext.getCmp("startDate3").getValue();
	      	var payment_type = Ext.getCmp("startDate32").getValue();
	      	var pay_receivables_code = Ext.getCmp("startDate1").getValue();
	      	var currency = Ext.getCmp("startDate31").getValue();
// 	      	var payment_type= Ext.getCmp("startDate1").getValue(); 
// 	      	 if(){
	      		 
// 	      	 }
	      	
			  var gridValue = Ext.getCmp("grid").getStore();
	      		var arr = [];
	      		 for (var i = 0; i < gridValue.data.length; i++) {
	      			var row = gridValue.getAt(i); 
	      			var bill_code = row.get("bill_code");  
	      			var amount = row.get("amount");
	      			obj = {
	      					bill_code:bill_code,
	      					amount:amount
	      			}
	      			arr.push(obj)
	      		 }
	      		var array = JSON.stringify(arr);
	      		Ext.Ajax.request({
	      			 url: '<%=basePath%>businessAccountApply/saveBusinessAccountApplyInfo',
	      			 params:{array:array,app_change_reason:app_change_reason,advance_pay_amount:advance_pay_amount,payment_type:payment_type,pay_receivables_code:pay_receivables_code,currency:currency},
	      			 method: 'POST',
	        			success: function () {
	        				Ext.MessageBox.alert("保存成功","保存成功");
	        				store.load();
//							javascript:location.reload();
							
	        			}
	      		});
		} },
<%-- 		{text: '删除', icon: '<%=basePath%>plugins/websocketInstantMsg/ext4/shared/icons/fam/cog_edit.png', handler: function (){ --%>
			
// 		} }
		],
	listeners: {
		'itemclick': function (view, record, item, index, e) {
// 			var store = Ext.getCmp('grid1').getStore();
// 			store.on('beforeload', function(store,options) {  
// 		            var new_params = {  
// 		            		check_id:record.data.check_id
// 		            };  

// 		            Ext.apply(store.proxy.extraParams,new_params);  
// 		        });  
// 		        store.load();  
		}
	},
	});
	
	var grid2 = Ext.create('Ext.grid.Panel', {
//	 		renderTo:'payment2',
		store: appCode,
		id:'grid2',
		forceFit: true,
		height: 250,
//			title:'成本付款审核单',
		stripeRows:true, 
		columnLines: true,
		selModel:Ext.create('Ext.selection.CheckboxModel',{checkOnly:false}),
		columns: [
			{text: 'id', dataIndex: 'id',hidden:true},
			{text: '收（付）款单号', dataIndex: 'payment_app_code'},
			{text: '收款单位', dataIndex: 'settlement_dept'},
			{text: '部门', dataIndex: 'payment_app_dept'},
			{text: '业务类型', dataIndex: 'dept_business_type'},
// 			{text: '收款单位', dataIndex: 'settlement_dept'},
			{text: '开户银行', dataIndex: 'due_bank'},
			{text: 'state', dataIndex: 'state',hidden:true},
		],
		bbar: [{
			xtype: 'pagingtoolbar',
			store: appCode,
			displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
			emptyMsg: "没有数据",
			beforePageText: "当前页",
			afterPageText: "共{0}页",
			displayInfo: true
		}],
		listeners:{  
			//单击  
			       'cellclick' : function(grid,row){  
			    	   var records = grid2.getSelectionModel().getSelection();
			    	   Ext.getCmp("startDate1").setValue(records[0].data.payment_app_code);
			    	   Ext.getCmp("startDate32").setValue(records[0].data.state);
// 			    	   Ext.getCmp("payment_bill_date1").setValue(Ext.util.Format.date(records[0].data.payment_bill_date, "Y-m-d"));
// 			    	   Ext.getCmp("payment_app_dept1").setValue(records[0].data.payment_app_dept);
// 			    	   Ext.getCmp("dept_business_type1").setValue(records[0].data.dept_business_type);
// 			    	   Ext.getCmp("startDate11").setValue(records[0].data.settlement_dept);
// 			    	   Ext.getCmp("startDate21").setValue(records[0].data.deposit_bank);
			    	   
// 			    	   Ext.getCmp("startDate31").setValue(records[0].data.bank_account);
// 			    	   Ext.getCmp("startDate41").setValue(records[0].data.remark);
// 			    	   Ext.getCmp("currency1").setValue(records[0].data.currency);
// 			    	   Ext.getCmp("paymentId1").setValue(records[0].data.paymentId);
			    	   
			    		
			    	   loan.on('beforeload', function(store,options) {  
				            var new_params = {  
				            		payment_app_code:records[0].data.payment_app_code,
				            		state:records[0].data.state
				            }; 
				            Ext.apply(loan.proxy.extraParams,new_params);  
				        });
			    	   
			    	   loan.load(); 
			    	   fandDao.close();
			       },  
			   }  
	});
	
	
	
	  var selForm = Ext.create('Ext.form.Panel', {
			title:'业务台账申请变更',
			name: 'selForm',
			renderTo:'payment',
			width:'100%',
			height:150,
//			collapsed :true,
// 			height:90,
//			collapsible:true,
			bodyStyle: {
				padding: '5px',
				border: 1
			},
			fieldDefaults: {
				labelAlign: 'left'
			},
			items: [c1]
		});
	  var selForm1 = Ext.create('Ext.form.Panel', {
// 			title:'业务台账申请变更',
			name: 'selForm1',
			renderTo:'payment1',
			width:'100%',
//			collapsed :true,
			height:140,
//			collapsible:true,
			bodyStyle: {
				padding: '5px',
				border: 1
			},
			fieldDefaults: {
				labelAlign: 'left'
			},
			items: [c2]
		});
	  var fandDao = Ext.create("Ext.window.Window",{
			title: "付款单号",
			modal:true,
			width: 600,
			height: 200,
			maximizable: true,
		    closeAction: 'hide',
			closeAction:'destroy',
			layout: "fit",
			closeAction :'hide',
//			margin: '3 3 0',
	        buttonAlign:'center',
	        items: [grid2]
	});
});

</script>
</html>